﻿using dreamwork.core.Infrastructure.Data.Cassandra;
using dreamwork.exam.api.Data;
using Dreamwork.Core.Infrastructure;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Threading.Tasks;
using System.Linq;

namespace dreamwork.exam.api.Controllers
{
	[Route("api/[controller]")]
	public class UserController : ControllerBase
	{
		private readonly ICanssandraRepository<ExamUser> _repository;
		public UserController(ICanssandraRepository<ExamUser> repository)
		{
			_repository = repository;
		}
		[HttpPost, Route("[action]")]
		public async Task<IActionResult> CreateUser(ExamUser user)
		{
			await _repository.AddAsync(new ExamUser() {Id=Guid.NewGuid(),Name="Jerry" });
			return Ok();
		}

		[HttpGet, Route("[action]/{id}")]
		public async Task<IActionResult> GetAsync(Guid id)
		{
			var items = await _repository.GetAllAsync();
			return Ok(await _repository.GetAsync(items.FirstOrDefault().Id));
		}
		[HttpPost, Route("[action]")]
		public async Task<IActionResult> GetListAsync(ExamUser user)
		{
			var items = await _repository.GetAllAsync();
			var item = items.FirstOrDefault();
			return Ok(await _repository.GetListAsync(x=> x.Id== item.Id));
		}

		[HttpGet, Route("[action]")]
		public async Task<IActionResult> GetAllAsync()
		{
			return Ok(await _repository.GetAllAsync());
		}

		
		[HttpPut, Route("[action]")]
		public async Task<IActionResult> UpdateAsync(ExamUser user)
		{
			await _repository.UpdateAsync(user);
			return Ok();
		}

		[HttpDelete, Route("[action]/{id}")]
		public async Task<IActionResult> RemoveAsync(Guid id)
		{
			var items = await _repository.GetAllAsync();
			var item = items.FirstOrDefault();
			await _repository.RemoveAsync(item.Id);
			return Ok(item);
		}
		[HttpDelete, Route("[action]")]
		public async Task<IActionResult> RemoveAllAsync()
		{
			await _repository.RemoveAllAsync();
			return Ok();
		}
	}
}
